﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.sendgift {
    import reprise.ui.*;
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import reprise.tweens.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import com.robertpenner.easing.*;
    import net.wooga.pets.external.*;

    public class GotGiftPopupMediator extends AbstractPopupMediator {

        public var friendsModel:FriendsModel;
        public var popupModel:PopupModel;
        private var gem_1_position:Array;
        private var gem_2_position:Array;
        private var gem_3_position:Array;
        private var gem_4_position:Array;
        private var _gemClips:Array;

        public function GotGiftPopupMediator(){
            this.gem_1_position = [new Point(0, 0), new Point(-25, -25)];
            this.gem_2_position = [new Point(25, 0), new Point(50, -25)];
            this.gem_3_position = [new Point(0, 25), new Point(-25, 50)];
            this.gem_4_position = [new Point(25, 25), new Point(50, 50)];
            this._gemClips = new Array();
            super();
        }
        override public function onRegister():void{
            super.onRegister();
            this.createSurprisePackage();
            addOkButton("global_button_send_gift_back");
            addLabel("headline myriadProFontNormal", "gift_popup_gift_surprise_package");
        }
        private function createSurprisePackage():void{
            var _local4:String;
            var _local1:MovieClip = AssetsLibrary.getMovieClip("IF_SurprisePackageAnimation");
            var _local2:Object = this.view.data;
            _local1.GemsLayer.x = (_local1.GemsLayer.x + 10);
            var _local3 = 1;
            for each (_local4 in _local2.rewards) {
                this._gemClips.push(this.createGem(_local1.GemsLayer, _local4, _local3));
                _local3++;
            };
            this.createFacebookImage(_local2.sender);
            RepriseUtils.addClipComponent(this.view, "surprisePackageDisplay", _local1);
            _local1.addEventListener(Event.ENTER_FRAME, this.surprisePackage_enterFrame);
        }
        private function surprisePackage_enterFrame(_arg1:Event):void{
            var _local2:MovieClip = MovieClip(_arg1.target);
            if (_local2.currentFrame > 45){
                _local2.removeEventListener(Event.ENTER_FRAME, this.surprisePackage_enterFrame);
                this.animateGems(_local2.GemsLayer);
            };
        }
        private function animateGems(_arg1:MovieClip):void{
            var _local3:MovieClip;
            var _local4:SimpleTween;
            var _local2 = 1;
            for each (_local3 in this._gemClips) {
                this.animateGem(_local3, _local2);
                _local2++;
            };
            _local4 = new SimpleTween(500);
            _local4.addTweenProperty(_arg1, "x", _arg1.x, (_arg1.x - 15), Quad.easeOut);
            _local4.addTweenProperty(_arg1, "y", _arg1.y, (_arg1.y - 15), Quad.easeOut);
            _local4.execute();
        }
        private function animateGem(_arg1:MovieClip, _arg2:int):void{
            var _local3:Array = this[(("gem_" + _arg2) + "_position")];
            var _local4:Point = _local3[1];
            if ((((_arg1.name == "RewardWater")) || ((_arg1.name == "RewardFire")))){
                _local4.x = (_local4.x + 8);
            };
            var _local5:SimpleTween = new SimpleTween(500);
            _local5.addTweenProperty(_arg1, "x", _arg1.x, _local4.x, Quad.easeOut);
            _local5.addTweenProperty(_arg1, "y", _arg1.y, _local4.y, Quad.easeOut);
            _local5.addTweenProperty(_arg1, "scaleX", _arg1.scaleX, 2, Quad.easeOut);
            _local5.addTweenProperty(_arg1, "scaleY", _arg1.scaleY, 2, Quad.easeOut);
            _local5.execute();
        }
        private function createGem(_arg1:MovieClip, _arg2:String, _arg3:int):MovieClip{
            var _local4:Array = this[(("gem_" + _arg3) + "_position")];
            var _local5:Point = _local4[0];
            var _local6:String = ("Inventory" + Strings.capitalize(_arg2));
            var _local7:MovieClip = AssetsLibrary.getMovieClip(_local6);
            _local7.x = _local5.x;
            _local7.y = _local5.y;
            _local7.name = ("Reward" + Strings.capitalize(_arg2));
            _arg1.addChild(_local7);
            _local7.scaleX = (_local7.scaleY = 1.2);
            return (_local7);
        }
        private function createFacebookImage(_arg1:String):void{
            var _local6:String;
            var _local7:String;
            var _local2:int;
            while (_local2 < this.friendsModel.friends.length) {
                if (this.friendsModel.friends[_local2].uid == _arg1){
                    _local6 = this.friendsModel.friends[_local2].picture;
                    _local7 = this.friendsModel.friends[_local2].first_name;
                    break;
                };
                _local2++;
            };
            if (!_local6){
                log(("i no friend found with user id: " + _arg1));
                return;
            };
            var _local3:UIComponent = RepriseUtils.addComponent(this.view, "scrollItemBackground");
            _local3.addCSSClass("friendBox");
            var _local4:UIComponent = RepriseUtils.addComponent(_local3, "imageDisplay");
            var _local5:FacebookImageResource = new FacebookImageResource(_local6);
            _local5.execute();
            RepriseUtils.addClipComponent(_local4, "image", _local5.loader());
            RepriseUtils.addLabel(_local3, "nameLabel", _local7);
        }
        override protected function onOk(_arg1:MouseEvent):void{
            if (!this.popupModel.hasNext()){
                dispatch(new InfoEvent(EventConstants.SHOW_GIFTS_POPUP, null));
            };
            super.onOk(_arg1);
        }
        override protected function onClose(_arg1:MouseEvent):void{
            super.onClose(_arg1);
        }
        private function get view():GotGiftPopupView{
            return ((viewComponent as GotGiftPopupView));
        }

    }
}//package net.wooga.pets.views.popups.sendgift 
